package com.zjitc.utils;

import lombok.Data;

import java.io.Serializable;

/**
 * @Author:徐莹
 * @Date:2025/04/14
 * 统一响应结果类
 */
@Data
public class ResponseResult<T> implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    /**
     * 状态码常量
     */
    public static final Integer SUCCESS = 200;           // 成功
    public static final Integer BAD_REQUEST = 400;       // 错误请求
    public static final Integer UNAUTHORIZED = 401;      // 未授权
    public static final Integer FORBIDDEN = 403;         // 禁止访问
    public static final Integer NOT_FOUND = 404;         // 资源未找到
    public static final Integer METHOD_NOT_ALLOWED = 405;// 方法不允许
    public static final Integer CONFLICT = 409;          // 冲突
    public static final Integer PARAM_ERROR = 422;       // 参数错误
    public static final Integer TOO_MANY_REQUESTS = 429; // 请求过多
    public static final Integer ERROR = 500;             // 服务器错误
    public static final Integer SERVICE_UNAVAILABLE = 503;// 服务不可用
    
    /**
     * 状态码
     */
    private Integer code;
    
    /**
     * 返回消息
     */
    private String message;
    
    /**
     * 返回数据
     */
    private T data;
    
    /**
     * 是否成功
     */
    private Boolean success;
    
    // 手动添加getter和setter方法，解决Lombok可能未生效的问题
    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    // 添加int参数版本的setCode
    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }
    
    /**
     * 成功返回结果
     * @param data 返回数据
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> success(T data) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(SUCCESS);
        result.setMessage("请求成功");
        result.setData(data);
        result.setSuccess(true);
        return result;
    }
    
    /**
     * 成功返回结果
     * @param message 返回消息
     * @param data 返回数据
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> success(String message, T data) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(SUCCESS);
        result.setMessage(message);
        result.setData(data);
        result.setSuccess(true);
        return result;
    }
    
    /**
     * 失败返回结果
     * @param message 错误消息
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> error(String message) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(ERROR);
        result.setMessage(message);
        result.setSuccess(false);
        return result;
    }
    
    /**
     * 失败返回结果
     * @param code 状态码
     * @param message 错误消息
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> error(Integer code, String message) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(code);
        result.setMessage(message);
        result.setSuccess(false);
        return result;
    }
    
    /**
     * 未授权返回结果
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> unauthorized() {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(UNAUTHORIZED);
        result.setMessage("请先登录后再访问");
        result.setSuccess(false);
        return result;
    }
    
    /**
     * 未授权返回结果
     * @param message 错误消息
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> unauthorized(String message) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(UNAUTHORIZED);
        result.setMessage(message);
        result.setSuccess(false);
        return result;
    }
    
    /**
     * 禁止访问返回结果
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> forbidden() {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(FORBIDDEN);
        result.setMessage("您的账号权限不足，无法执行此操作");
        result.setSuccess(false);
        return result;
    }
    
    /**
     * 禁止访问返回结果
     * @param message 错误消息
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> forbidden(String message) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(FORBIDDEN);
        result.setMessage(message);
        result.setSuccess(false);
        return result;
    }
    
    /**
     * 资源未找到返回结果
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> notFound() {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(NOT_FOUND);
        result.setMessage("请求的资源不存在");
        result.setSuccess(false);
        return result;
    }
    
    /**
     * 资源未找到返回结果
     * @param message 错误消息
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> notFound(String message) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(NOT_FOUND);
        result.setMessage(message);
        result.setSuccess(false);
        return result;
    }
    
    /**
     * 参数错误返回结果
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> paramError() {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(PARAM_ERROR);
        result.setMessage("请求参数错误，请检查输入");
        result.setSuccess(false);
        return result;
    }
    
    /**
     * 参数错误返回结果
     * @param message 错误消息
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> paramError(String message) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(PARAM_ERROR);
        result.setMessage(message);
        result.setSuccess(false);
        return result;
    }
    
    /**
     * 请求过多返回结果
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> tooManyRequests() {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(TOO_MANY_REQUESTS);
        result.setMessage("请求过于频繁，请稍后再试");
        result.setSuccess(false);
        return result;
    }
} 